package com.hq.fxh.No5_User.Jianli.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hq.fxh.CustomerView.PopupWindow.PopupForAddress;
import com.hq.fxh.CustomerView.PopupWindow.PopupForChangeInfo;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Modle.JianLiWish;
import com.hq.fxh.Modle.JianLiWishGetModle;
import com.hq.fxh.No5_User.CitySelectActivity;
import com.hq.fxh.No6UerSpace.Fragment.Select;
import com.hq.fxh.R;
import com.hq.fxh.Utils.AppConstant;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;
import com.hq.fxh.Utils.Tool;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/9/21
 * @Email xlwenhao1858@sina.com
 */


public class Fragment_JianLi_QiuZhiYIXiang extends Fragment implements View.OnClickListener {


    protected View rootView;
    protected EditText editJianliQiwangShuoming;
    protected TextView tvJianliQiwangZhiWei;
    protected TextView tvJianliQiwangCity;
    protected TextView tvJianliQiwangGongZhi;
    protected ImageView toolbarImageBack;
    protected LinearLayout toolbarLayoutBack;
    protected TextView toolBarTvTitle;
    protected TextView toolBarTvMore;
    protected LinearLayout toolbarLayoutMore;
    protected LinearLayout toolbarContent;
    private PopupForChangeInfo popup;
    int type = 0;
    private JianLiWish jianLiWish;
    private PopupForAddress popupXueli;
    private int memberId;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_jianliset_yixiang, null);
        initView(rootView);
        initData();
        getWorkWish();
        return rootView;
    }

    private void initData() {
        memberId = SharedPreferenceUtils.getMemberId(getContext());

        jianLiWish = new JianLiWish();
        toolBarTvTitle.setText("求职意向");
        toolBarTvMore.setText("保存");
        if (popup == null) {
            popup = new PopupForChangeInfo(getActivity(), new PopupForChangeInfo.ChangeCallBack() {
                @Override
                public void call(String comment) {
                    if (comment == null || comment.equals("")) {
                        LogUtils.startToash(getContext(), "填写的信息为空");
                    } else {
                        switch (type) {
                            case 0:
                                tvJianliQiwangZhiWei.setText(comment);
                                jianLiWish.setJob_name(comment);
                                break;
                            case 1:
                                tvJianliQiwangGongZhi.setText(comment);
                                //    jianLiWish.setEnd_money(comment);

                                break;


                        }
                    }


                }
            });
        }
    }

    private void initView(View rootView) {
        tvJianliQiwangZhiWei = (TextView) rootView.findViewById(R.id.tv_jianli_qiwang_ZhiWei);
        tvJianliQiwangZhiWei.setOnClickListener(Fragment_JianLi_QiuZhiYIXiang.this);
        tvJianliQiwangCity = (TextView) rootView.findViewById(R.id.tv_jianli_qiwang_City);
        tvJianliQiwangCity.setOnClickListener(Fragment_JianLi_QiuZhiYIXiang.this);
        tvJianliQiwangGongZhi = (TextView) rootView.findViewById(R.id.tv_jianli_qiwang_GongZhi);
        tvJianliQiwangGongZhi.setOnClickListener(Fragment_JianLi_QiuZhiYIXiang.this);
        editJianliQiwangShuoming = (EditText) rootView.findViewById(R.id.edit_jianli_qiwang_shuoming);
        toolbarImageBack = (ImageView) rootView.findViewById(R.id.toolbar_image_back);
        toolbarLayoutBack = (LinearLayout) rootView.findViewById(R.id.toolbar_Layout_back);
        toolbarLayoutBack.setOnClickListener(Fragment_JianLi_QiuZhiYIXiang.this);
        toolBarTvTitle = (TextView) rootView.findViewById(R.id.toolBar_tv_Title);
        toolBarTvMore = (TextView) rootView.findViewById(R.id.toolBar_tv_More);
        toolbarLayoutMore = (LinearLayout) rootView.findViewById(R.id.toolbar_Layout_More);
        toolbarLayoutMore.setOnClickListener(Fragment_JianLi_QiuZhiYIXiang.this);
        toolbarContent = (LinearLayout) rootView.findViewById(R.id.toolbar_Content);


    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_jianli_qiwang_ZhiWei) {
            clickZhiWei();


        } else if (view.getId() == R.id.tv_jianli_qiwang_City) {
            clickCity();
        } else if (view.getId() == R.id.tv_jianli_qiwang_GongZhi) {
            clickGongZhi();

        } else if (view.getId() == R.id.toolbar_Layout_back) {
            getActivity().finish();

        } else if (view.getId() == R.id.toolbar_Layout_More) {
            clickBaoCun();

        }
    }

    private void clickBaoCun() {

        if (Tool.editIsEmpty(editJianliQiwangShuoming)) {

        } else {
            jianLiWish.setOther(editJianliQiwangShuoming.getText().toString());
        }

        addWorkWish();

    }

    private void clickZhiWei() {
        showEdit(tvJianliQiwangZhiWei, 0, AppConstant.请输入期望职位);
    }

    private void clickCity() {
        startActivityForResult(new Intent(getContext(), Select.class), 110);

    }

    private void clickGongZhi() {
 /*
        2000以下

        2000－5000

        5000－10000

        10000-15000

        15000-25000

        25000-50000

        50000以上*/
        if (popupXueli == null) {
            String[] xueli = {"2000以下", "2000－5000", " 5000－10000", "10000-15000", "15000-25000", "  25000-50000", "50000以上"};
            popupXueli = new PopupForAddress(getActivity(), xueli, new PopupForAddress.ShareModleCallBack() {
                @Override
                public void callSure(int position, String comment) {
                    popupXueli.dismiss();
                    tvJianliQiwangGongZhi.setText(comment);
                    switch (position) {
                        case 0:
                            jianLiWish.setStart_money(0);
                            jianLiWish.setEnd_money(2000);
                            break;
                        case 1:
                            jianLiWish.setEnd_money(5000);
                            jianLiWish.setStart_money(2000);
                            break;
                        case 2:
                            jianLiWish.setEnd_money(10000);
                            jianLiWish.setStart_money(5000);
                            break;
                        case 3:
                            jianLiWish.setEnd_money(15000);
                            jianLiWish.setStart_money(10000);
                            break;
                        case 4:
                            jianLiWish.setEnd_money(25000);
                            jianLiWish.setStart_money(15000);
                            break;
                        case 5:
                            jianLiWish.setEnd_money(50000);
                            jianLiWish.setStart_money(25000);
                            break;
                        case 6:
                            jianLiWish.setEnd_money(100000);
                            jianLiWish.setStart_money(50000);
                            break;
                    }


                }
            });
        }

        popupXueli.show(tvJianliQiwangGongZhi);

    }

    public void showEdit(View view, int type, String title) {
        this.type = type;
        popup.setTitle(title);
        popup.openKeyboard();
        Tool.setAlpha(getActivity(), 0.6f);
        popup.showAtLocation(view,
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    public void addWorkWish() {
        jianLiWish.setMember_id(memberId);
        String s = new Gson().toJson(jianLiWish);
        LogUtils.e("期望参数：" + s);
        UserServiceImpl.updateResumeWish(s, memberId + "", new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String string = null;
                try {
                    ResponseBody body = response.body();
                    if (body == null) {
                        ResponseBody responseBody = response.errorBody();
                        if (responseBody != null) {
                            String string1 = responseBody.string();
                            LogUtils.e("错误信息1" + string1);
                        }
                    } else {
                        string = body.string();
                        LogUtils.e(string);
                        getActivity().finish();
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                LogUtils.e("错误信息" + t.getMessage());

            }
        });

    }


    public void getWorkWish() {

        UserServiceImpl.getResumeWish(memberId + "", new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String string = null;
                try {
                    ResponseBody body = response.body();
                    if (body == null) {
                        ResponseBody responseBody = response.errorBody();
                        if (responseBody != null) {
                            String string1 = responseBody.string();
                            LogUtils.e("错误信息" + string1);
                        }
                    } else {
                        string = body.string();
                        LogUtils.e("期望" + string);
                        JianLiWishGetModle jianLiWishGetModle = new Gson().fromJson(string, JianLiWishGetModle.class);
                        JianLiWishGetModle.DataBean data = jianLiWishGetModle.getData();
                        setViewData(data);


                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                LogUtils.e("错误信息" + t.getMessage());

            }
        });

    }

    private void setViewData(JianLiWishGetModle.DataBean data) {
        Tool.setStringToText(tvJianliQiwangCity, data.getCity_name());
        Tool.setStringToText(tvJianliQiwangZhiWei, data.getJob_name());
        editJianliQiwangShuoming.setText(data.getOther());
        //     "2000以下", "2000－5000", " 5000－10000", "10000-15000", "15000-25000", "  25000-50000", "50000以上"
        switch ((int) data.getEnd_money()) {
            case 0:
                Tool.setStringToText(tvJianliQiwangGongZhi, "2000以下");

                break;
            case 2000:
                Tool.setStringToText(tvJianliQiwangGongZhi, "2000－5000");

                break;
            case 5000:
                Tool.setStringToText(tvJianliQiwangGongZhi, " 5000－10000");

                break;
            case 10000:
                Tool.setStringToText(tvJianliQiwangGongZhi, "10000-15000");

                break;
            case 15000:
                Tool.setStringToText(tvJianliQiwangGongZhi, "15000-25000");

                break;
            case 25000:
                Tool.setStringToText(tvJianliQiwangGongZhi, "25000-50000");

                break;
            case 50000:
                Tool.setStringToText(tvJianliQiwangGongZhi, "50000以上");

                break;


        }


    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 110 && data != null) {

            int proID = data.getIntExtra("proID", -1);
            int cityID = data.getIntExtra("cityID", -1);
            String cityName = data.getStringExtra("cityName");
            String proName = data.getStringExtra("proName");
            LogUtils.e(proName + "  " + proID + "  " + cityName + " " + cityID);

            jianLiWish.setCity_id(cityID);
            jianLiWish.setCity_name(cityName);
            jianLiWish.setProvince_name(proName);
            jianLiWish.setProvince_id(proID);

            tvJianliQiwangCity.setText(cityName);

        }
    }
}
